// 基于axios封装
import axios from 'axios'

const instance = axios.create({
  baseURL: 'http://pcapi-xiaotuxian-front-devtest.itheima.net',
  timeout: 5000
})

instance.interceptors.request.use(config => {
// Do something before request is sent
  return config
}, error => {
// Do something with request error
  return Promise.reject(error)
})

instance.interceptors.response.use(response => {
// Do something before response is sent
  return response
}, error => {
// Do something with response error
  return Promise.reject(error)
})

/*
    用法：
       request('url','GET',{name:'cp'}).then(res => console.log(res))
    优化：
        如果使用者传过来的method是  get或者Get。。。。
*/
function request (url, method, reqData) {
  return instance({
    url,
    method,
    // 兼容对象key
    [method.toLocaleLowerCase() === 'get' ? 'params' : 'data']: reqData
  })
}

export default request
